package com.woniu.service.impl;

import com.woniu.outlet.po.User;
import com.woniu.outlet.po.UserExample;
import com.woniu.outlet.mapper.UserMapper;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User getUser (String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        if(users.size()==0){
            throw new UsernameNotFoundException("账号不存在!");
        }
        return users.get(0);
    }


    @Override
    public void userAdd (User user) {
        user.setStatus("1");
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userMapper.insertSelective(user);
    }

    @Override
    public void userUpdate (User user) {
        if("true".equals(user.getStatus())){
            user.setStatus("y");
        }else{
            user.setStatus("n");
        }
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void userDel (Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User getUserById (Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }



    @Override
    public UserDetails loadUserByUsername (String username) throws UsernameNotFoundException {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        if(users.size()==0){
//            throw new UsernameNotFoundException("账号不存在!");
            return null;
        }
        User user = users.get(0);
        //权限集合
        List<GrantedAuthority> authorities = new ArrayList<>();
        List<String> strings = userMapper.getUserPercodeByUser(user.getUsername());
        ValueOperations ops = redisTemplate.opsForValue();
        List<GrantedAuthority> authoritiesList = (List)ops.get(user.getUsername());
        if(StringUtils.isEmpty(authoritiesList)){
            strings.forEach(str->{
                authorities.add(new SimpleGrantedAuthority(str));
            });
            UserDetails userDetails = null;
            if(user.getStatus().equals("0")){
                userDetails =
                        new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),true,true,true,false,authorities);
            }else{
                userDetails =
                        new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),authorities);
            }
            return userDetails;
        }else {
            UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),authoritiesList);
            return  userDetails;
        }
    }
}
